package com.camelot.boot.module.system.controller.admin.companyrequirement;

import com.camelot.boot.framework.security.core.util.SecurityFrameworkUtils;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.camelot.boot.framework.common.pojo.PageParam;
import com.camelot.boot.framework.common.pojo.PageResult;
import com.camelot.boot.framework.common.pojo.CommonResult;
import com.camelot.boot.framework.common.util.object.BeanUtils;
import static com.camelot.boot.framework.common.pojo.CommonResult.success;

import com.camelot.boot.framework.excel.core.util.ExcelUtils;

import com.camelot.boot.framework.apilog.core.annotation.ApiAccessLog;
import static com.camelot.boot.framework.apilog.core.enums.OperateTypeEnum.*;

import com.camelot.boot.module.system.controller.admin.companyrequirement.vo.*;
import com.camelot.boot.module.system.dal.dataobject.companyrequirement.CompanyRequirementDO;
import com.camelot.boot.module.system.service.companyrequirement.CompanyRequirementService;

@Tag(name = "管理后台 - 用户公司需求配置信息")
@RestController
@RequestMapping("/system/company-requirement")
@Validated
public class CompanyRequirementController {

    @Resource
    private CompanyRequirementService companyRequirementService;

    @PostMapping("/create")
    @Operation(summary = "创建用户公司需求配置信息")
    @PreAuthorize("@ss.hasPermission('system:company-requirement:create')")
    public CommonResult<Long> createCompanyRequirement(@Valid @RequestBody CompanyRequirementSaveReqVO createReqVO) {
        createReqVO.setUserId(SecurityFrameworkUtils.getLoginUserId());
        return success(companyRequirementService.createCompanyRequirement(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户公司需求配置信息")
    @PreAuthorize("@ss.hasPermission('system:company-requirement:update')")
    public CommonResult<Boolean> updateCompanyRequirement(@Valid @RequestBody CompanyRequirementSaveReqVO updateReqVO) {
        companyRequirementService.updateCompanyRequirement(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户公司需求配置信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:company-requirement:delete')")
    public CommonResult<Boolean> deleteCompanyRequirement(@RequestParam("id") Long id) {
        companyRequirementService.deleteCompanyRequirement(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户公司需求配置信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:company-requirement:query')")
    public CommonResult<CompanyRequirementRespVO> getCompanyRequirement(@RequestParam("id") Long id) {
        CompanyRequirementDO companyRequirement = companyRequirementService.getCompanyRequirement(id);
        return success(BeanUtils.toBean(companyRequirement, CompanyRequirementRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户公司需求配置信息分页")
    @PreAuthorize("@ss.hasPermission('system:company-requirement:query')")
    public CommonResult<PageResult<CompanyRequirementRespVO>> getCompanyRequirementPage(@Valid CompanyRequirementPageReqVO pageReqVO) {
        PageResult<CompanyRequirementDO> pageResult = companyRequirementService.getCompanyRequirementPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CompanyRequirementRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户公司需求配置信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:company-requirement:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCompanyRequirementExcel(@Valid CompanyRequirementPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CompanyRequirementDO> list = companyRequirementService.getCompanyRequirementPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户公司需求配置信息.xls", "数据", CompanyRequirementRespVO.class,
                        BeanUtils.toBean(list, CompanyRequirementRespVO.class));
    }

}